package com.taomu.app.zongchouhuli.utils;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;
import android.util.Log;

import com.alibaba.fastjson.JSON;

import java.util.ArrayList;
import java.util.List;

/**
 * 数据本地存储简单使用shareprefence
 */
public class StorageHelper {
    private static SharedPreferences share;

    public static void init(Context mContext){
        share = mContext.getSharedPreferences("zonglihuchou",Activity.MODE_PRIVATE);
    }

    public static void saveStringValue(String keys,String values){

    }

    public static void saveToken(String token){
        SharedPreferences.Editor editor = share.edit();
        editor.putString(StorageKeys.TOKENKEYS,token);
        editor.commit();
    }

    public static void removeToken(){
        SharedPreferences.Editor editor = share.edit();
        editor.remove(StorageKeys.TOKENKEYS);
        editor.commit();
    }

    public static void saveUid(String uid){
        SharedPreferences.Editor editor = share.edit();
        editor.putString(StorageKeys.ACCOUNTUID, uid);
        editor.commit();
    }

    public static String getUid(){
        String uid = share.getString(StorageKeys.ACCOUNTUID,"");
        return uid;
    }

    public static void removeUid(){
        SharedPreferences.Editor editor = share.edit();
        editor.remove(StorageKeys.ACCOUNTUID);
        editor.commit();
    }

    public static void saveAccountHis(String accounts){
        SharedPreferences.Editor editor = share.edit();
        editor.putString(StorageKeys.ACCOUNTHIS, accounts);
        editor.commit();
    }

    public static void saveAccountInfo(String accountinfo){
        SharedPreferences.Editor editor = share.edit();
        editor.putString(StorageKeys.ACCOUNTINFO, accountinfo);
        editor.commit();
    }

    public static void saveImportNotice(String accountinfo){
        SharedPreferences.Editor editor = share.edit();
        editor.putString(StorageKeys.IMPORTNOTICE, accountinfo);
        editor.commit();
    }

    public static List<String> getImportNotice(){
        String accounts = share.getString(StorageKeys.IMPORTNOTICE,"");
        List<String> accountslist = new ArrayList<>();
        if(!TextUtils.isEmpty(accounts)){
            accountslist = JSON.parseArray(accounts,String.class);
        }
        return accountslist;
    }

    public static List<String> getAccountsHis(){
        String accounts = share.getString(StorageKeys.ACCOUNTHIS,"");
        List<String> accountslist = new ArrayList<>();
        if(!TextUtils.isEmpty(accounts)){
            accountslist = JSON.parseArray(accounts,String.class);
        }
        return accountslist;
    }

    public static String getAccountInfo(){
        return share.getString(StorageKeys.ACCOUNTINFO,"");
    }

    public static String getToken(){
        String token = share.getString(StorageKeys.TOKENKEYS,"");
        return token;
    }

    public static boolean isLogin(){
        return !TextUtils.isEmpty(getToken());
    }

    public static void clearToken(){
        SharedPreferences.Editor editor = share.edit();
        editor.putString(StorageKeys.TOKENKEYS,"");
        editor.commit();
    }

    public static void clearAccountInfo(){
        SharedPreferences.Editor editor = share.edit();
        editor.putString(StorageKeys.ACCOUNTINFO,"");
        editor.commit();
    }

    public static void clearAccountsHis(){
        SharedPreferences.Editor editor = share.edit();
        editor.putString(StorageKeys.ACCOUNTHIS,"");
        editor.commit();
    }

    //清除数据
    public static void clear() {
        SharedPreferences.Editor editor = share.edit();
        editor.clear();
        editor.commit();
    }

    public static void saveHttpNewDownload(String url){
        SharedPreferences.Editor editor = share.edit();
        editor.putString("newurl", url);
        editor.commit();
    }

    public static String getHttpNewDownload(){
        String token = share.getString("newurl","");
        return token;
    }
}
